package com.lx.apk.encrypt.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * date 2020/8/10.
 * description：
 */
public class CmdUtils {
    public static void execCommand(String command) {
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        BufferedReader success_buffer = null;
        BufferedReader error_buffer = null;
        try {
            process = runtime.exec(command);
            String line;
            success_buffer = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = success_buffer.readLine()) != null) {
                LogUtils.i(line);
            }
            process.waitFor();
            if (process.exitValue() != 0) {
                LogUtils.e("exec fail --- " + command);
                error_buffer = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                while ((line = error_buffer.readLine()) != null) {
                    LogUtils.e(line);
                }
            } else {
                LogUtils.i("exec success --- " + command);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
                process = null;
            }
            if (success_buffer != null) {
                try {
                    success_buffer.close();
                    success_buffer = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (error_buffer != null) {
                try {
                    error_buffer.close();
                    error_buffer = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
